# @Time:   2021/2/8 9:34
# @Author: Zong Wei
import time
from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class BizAreaPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务区域管理']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")

    area_name = (By.ID, "AreaName")
    area_code = (By.ID, "AreaCode")
    city = (By.ID, "_easyui_textbox_input3")
    city_value = (By.ID, "datagrid-row-r3-2-0")
    province_id = (By.ID, "ProvinceID")
    city_id = (By.ID, "CityID")
    remark = (By.ID, "Remark")
    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='所在省份']//following::tr[1]")
    current_time = str(int(time.time()))

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def biz_area_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.driver.execute_script('$("#admui-navTabs").scrollTop(1000)')
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def add_area(self, city='上海市'):
        global area_name
        area_name = 'AREA NAME'+self.current_time
        global area_code
        area_code = 'AREA CODE'+self.current_time
        self.driver.switch_to.frame(1)
        self.wait.until(ec.element_to_be_clickable(self.add_button)).click()
        self.logger.info("添加业务区域")
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.presence_of_element_located(self.area_name)).send_keys(area_name)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.area_code)).clear()
        self.wait.until(ec.presence_of_element_located(self.area_code)).send_keys(area_code)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.city)).send_keys(city)
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.city_value)).click()
        sleep(1)

        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        sleep(1)
        self.logger.info("新增完成")

    def search_area(self, city='上海'):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.province_id)).send_keys(city)
        self.wait.until(ec.presence_of_element_located(self.city_id)).send_keys(city)
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.area_name)).send_keys(area_name)
        self.wait.until(ec.presence_of_element_located(self.area_code)).send_keys(area_code)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def edit_area(self):
        self.search_area()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        # self.wait.until(ec.presence_of_element_located(self.area_code)).send_keys('_Ed')
        self.wait.until(ec.presence_of_element_located(self.remark)).send_keys(Faker().address())
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("编辑修改完成")

    def del_area(self):
        self.search_area()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.del_button)).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("删除成功")


if __name__ == '__main__':
    pg = BizAreaPage()
    pg.biz_area_home()
    pg.add_area()

